//
//  XKNewFeatureViewController.swift
//  XKWeibo
//
//  Created by 小k on 15/11/10.
//  Copyright © 2015年 小k. All rights reserved.
//

import UIKit


/// 设置celecitonViewCell的重用标识
private let XKNewFeatureViewCellID = "XKNewFeatureViewCellID"

/// 设置新特性界面的数量
private let XKNewFeatureCount = 4


class XKNewFeatureViewController: UICollectionViewController {
    
    // MARK: - 构造方法
    init(){
        let layout = UICollectionViewFlowLayout()
        
        /// 布局cell
        layout.itemSize = UIScreen.mainScreen().bounds.size
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        layout.scrollDirection = .Horizontal        // 水平滑动
        super.init(collectionViewLayout:layout)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override  func viewDidLoad() {
        super.viewDidLoad()
        prepareCollectionView()
    }
    
    /// 隐藏状态栏
    override func prefersStatusBarHidden() -> Bool {
        return true
    }
    
    /// 准备collectionView
    private func prepareCollectionView () {
        collectionView?.backgroundColor = UIColor.redColor()
        collectionView?.bounces = false                         // 取消弹簧效果
        collectionView?.pagingEnabled = true                    // 设置分页效果
        collectionView?.showsHorizontalScrollIndicator = false  //取消水平滑动滚动条
        
        /// 注册重用cell
        collectionView?.registerClass(XKNewFeatureViewCell.self, forCellWithReuseIdentifier: XKNewFeatureViewCellID)
    }
}

// MARK: - 数据源方法
extension XKNewFeatureViewController {

    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return XKNewFeatureCount
    }
   
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> XKNewFeatureViewCell  {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(XKNewFeatureViewCellID, forIndexPath: indexPath) as! XKNewFeatureViewCell
        cell.imageIndex = indexPath.item
        cell.backgroundColor = UIColor.whiteColor()
        
        return cell
    }
    
    
    /// ScrollView停止滚动方法
    override func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        let page = Int( scrollView.contentOffset.x / scrollView.bounds.width)
        
        if page != XKNewFeatureCount - 1 {
            return
        }
        
        let cell = collectionView?.cellForItemAtIndexPath(NSIndexPath(forItem: page, inSection: 0)) as! XKNewFeatureViewCell
        
        // 显示动画
        cell.showBtnAnimation()

}

}
